package com.zw.mes.module.shipments.service;


import com.zw.mes.entity.shipments.InstDispatchingDetail;
import com.zw.mes.entity.shipments.InstDispatchingFile;
import com.zw.mes.entity.shipments.InstDispatchingList;
import com.zw.mes.module.cost.model.Project;
import com.zw.mes.module.shipments.mapper.InstDispatchingCreateMapper;
import com.zw.mes.module.shipments.repository.InstDispatchingDetailRepository;
import com.zw.mes.module.shipments.repository.InstDispatchingFileRepository;
import com.zw.mes.module.shipments.repository.InstDispatchingRepository;
import com.zw.mes.module.system.service.LogService;
import com.zw.mes.util.FileUtils;
import com.zw.mes.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: China.wsg
 * @Date: 2020/11/10 15:11
 */
@Service
@SuppressWarnings("all")
public class InstDispatchingCreateService {


    @Autowired
    private LogService logService;
    @Autowired
    private InstDispatchingRepository instDispatchingRepository;
    @Autowired
    private InstDispatchingDetailRepository instDispatchingDetailRepository;
    @Autowired
    private InstDispatchingCreateMapper instDispatchingCreateMapper;
    @Value("${user.upload.path}")
    private String uploadPath;
    @Autowired
    private InstDispatchingFileRepository instDispatchingFileRepository;

    /**
     * 保存安装派工单
     *
     * @param instDispatchingList
     * @param special
     * @param installPrincipal
     * @param installTime
     * @param isDebugging
     * @param installRemark
     * @return
     * @throws ParseException
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveAll(InstDispatchingList instDispatchingList, String[] special, String[] installPrincipal,
                       String[] installTime, Integer[] isDebugging, String[] installRemark) throws ParseException {
        instDispatchingList.setIsDel(0);
        instDispatchingList.setAuthor(ShiroUtils.getUser().getName());
        instDispatchingList.setAuthorTime(new Date());
        instDispatchingList.setUpdateName(ShiroUtils.getUser().getName());
        instDispatchingList.setUpdateTime(new Date());
        instDispatchingList.setSubStatus(0);
        instDispatchingList.setDebuggingStatu("1");
        instDispatchingList.setInstallStatu("1");
        int k = 0;
        for (int i = 0; i < isDebugging.length; i++) {
            if (isDebugging[i]>0){
                k++;
            }
        }
        if (k>0){
            instDispatchingList.setIsDebugging(1);
        }else {
            instDispatchingList.setIsDebugging(2);
        }
        InstDispatchingList instDispatchingList1 = instDispatchingRepository.save(instDispatchingList);
        logService.saveLog("创建安装派工单主单", "项目编号：" + instDispatchingList1.getProCode() + "<br>主键ID：" + instDispatchingList1.getId());
        for (int i = 0; i < special.length; i++) {
            InstDispatchingDetail instDispatchingDetail = new InstDispatchingDetail();
            instDispatchingDetail.setSheetId(instDispatchingList1.getId());
            instDispatchingDetail.setSpecial(special[i]);
            instDispatchingDetail.setInstallPrincipal(installPrincipal[i]);
            instDispatchingDetail.setInstallTime(StrToDate(installTime[i]));
            instDispatchingDetail.setIsDebugging(isDebugging[i]);
            if (isDebugging[i]==2){
                instDispatchingDetail.setDebuggingStatus("0");
            }else {
                instDispatchingDetail.setDebuggingStatus("1");
            }
            instDispatchingDetail.setInstallStatus("1");
            instDispatchingDetail.setInstallRemark(installRemark[i]);
            instDispatchingDetail.setIsDel(0);
            InstDispatchingDetail instDispatchingDetail1=  instDispatchingDetailRepository.save(instDispatchingDetail);
            logService.saveLog("创建安装派工单明细单", "id：" + instDispatchingDetail1.getId() + "<br>专业：" + instDispatchingDetail1.getSpecial());
        }
        return 1;
    }

    /**
     *  
     * 字符串转换成日期 
     *
     * @param str 
     * @return date 
     */

    public static Date StrToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 调试任务反馈列表
     * @param instDispatchingList
     * @return
     */
    public List<InstDispatchingList> findinstDispatchingQuery(InstDispatchingList instDispatchingList) {
        return  instDispatchingCreateMapper.findinstDispatchingQuery(instDispatchingList);
    }

    /**
     * 调试反馈明细
     * @param instDispatchingDetail
     * @return
     */
    public List<InstDispatchingDetail> findinstDispatchingDetailQuery(InstDispatchingDetail instDispatchingDetail) {
        return  instDispatchingCreateMapper.findinstDispatchingDetailQuery(instDispatchingDetail);
    }

    /**
     * 维护调试进度
     * @param instDispatchingDetail
     * @return
     */
    public Integer saveInstDispatchingDetail(InstDispatchingDetail instDispatchingDetail) {
        InstDispatchingDetail instDispatchingDetail1 = instDispatchingDetailRepository.getOne(instDispatchingDetail.getId());
        instDispatchingDetail1.setDebuggingEndTime(instDispatchingDetail.getDebuggingEndTime());
        instDispatchingDetail1.setDebuggingName(ShiroUtils.getUser().getName());
        instDispatchingDetail1.setDebuggingTime(new Date());
        instDispatchingDetail1.setDebuggingRemark(instDispatchingDetail.getDebuggingRemark());
        instDispatchingDetail1.setDebuggingStartTime(instDispatchingDetail.getDebuggingStartTime());
        instDispatchingDetail1.setDebuggingStatus(instDispatchingDetail.getDebuggingStatus());
        instDispatchingDetailRepository.save(instDispatchingDetail1);
        logService.saveLog("维护调试进度", "id：" + instDispatchingDetail1.getId() + "<br>专业：" + instDispatchingDetail1.getSpecial());
        //更新主表状态
        InstDispatchingList instDispatchingList = instDispatchingRepository.getOne(instDispatchingDetail1.getSheetId());
        List<InstDispatchingDetail> instDispatchingDetails = instDispatchingCreateMapper.findinstDispatchingDetailQuery(instDispatchingDetail1);
        int status=0;
        for (int i = 0; i < instDispatchingDetails.size(); i++) {
            if (instDispatchingDetails.get(i).getDebuggingStatus().equals("1")){
                status++;
            }
            if (instDispatchingDetails.get(i).getDebuggingStatus().equals("2")){
                status+=10;
            }
        }
        if (status>9){
            instDispatchingList.setDebuggingStatu("2");
        }else if (status<=3){
            instDispatchingList.setDebuggingStatu("1");
        }else {
            instDispatchingList.setDebuggingStatu("3");
        }
        instDispatchingRepository.save(instDispatchingList);
        return 1;
    }

    /**
     * 查询调试文件
     * @param instDispatchingFile
     * @return
     */
    public List<InstDispatchingFile> findinstDispatchingFileQuery(InstDispatchingFile instDispatchingFile) {
        return  instDispatchingCreateMapper.findinstDispatchingFileQuery(instDispatchingFile);
    }

    /**
     * 文件上传
     * @param instDispatchingFile
     * @param file
     */
    public void uploadFileExp(InstDispatchingFile instDispatchingFile, MultipartFile file) {
        // 取得项目信息
        String path = uploadPath + instDispatchingFile.getSheetId() + "/" + "proExplain/";
        String fileName = FileUtils.upload(file, path);
        String url = "/files/" + instDispatchingFile.getSheetId() + "/" + "proExplain/" + fileName;
        instDispatchingFile.setFileName(fileName);
        instDispatchingFile.setIsDel(0);
        instDispatchingFile.setUploadName(ShiroUtils.getUser().getName());
        instDispatchingFile.setUploadTime(new Date());
        instDispatchingFile.setUrl(url);
        InstDispatchingFile instDispatchingFile1 = instDispatchingFileRepository.save(instDispatchingFile);
        logService.saveLog("上传附件", "id："+instDispatchingFile1.getId() + "<br>" + fileName);
    }

    /**
     * 修改说明文件
     * @param instDispatchingFile
     * @return
     */
    public int updExplainById(InstDispatchingFile instDispatchingFile) {
        InstDispatchingFile instDispatchingFile1 = instDispatchingFileRepository.getOne(instDispatchingFile.getId());
        instDispatchingFile1.setRemark(instDispatchingFile.getRemark());
        instDispatchingFileRepository.save(instDispatchingFile1);
        logService.saveLog("修改附件描述", "id："+instDispatchingFile1.getId() + "<br>" + instDispatchingFile.getRemark());
        return 1;
    }

    /**
     * 删除附件
     * @param instDispatchingFile
     * @return
     */
    public int delExp(InstDispatchingFile instDispatchingFile) {
        InstDispatchingFile instDispatchingFile1 = instDispatchingFileRepository.getOne(instDispatchingFile.getId());
        instDispatchingFile1.setIsDel(1);
        instDispatchingFile1.setUploadTime(new Date());
        instDispatchingFile1.setUploadName(ShiroUtils.getUser().getName());
        instDispatchingFileRepository.save(instDispatchingFile1);
        logService.saveLog("删除附件", "id："+instDispatchingFile1.getId() + "<br>删除人：" + ShiroUtils.getUser().getName());
        return 1;
    }
}
